<template>
	<view class="content">
		<u-navbar :fixed="false" :title="pageParam.type +'筛选'" :autoBack="true" :safeAreaInsetTop="true"
			leftIconColor="#fff">
		</u-navbar>


		<view class="MainBox">
			<u-collapse accordion v-model="OpenCollapseData">
				<u-collapse-item :title="item[pageParam.serverSearch.showText_Fahter]" :name="item.ID"
					v-for="item in ListData" :key="item.ID">
					<view style="padding: 0 20rpx;">
						<view v-for="row in item.Child" :key="row.ID" @click="ChoiceCell(item,row)"
							class="flex_center_vertical"
							style="padding: 16rpx 0;border-bottom: 1px solid #e8e8e8;justify-content: space-between;">
							<text class="u-collapse-content">{{row[pageParam.serverSearch.showText_Son]}}</text>

							<uni-icons type="checkmarkempty" color="#14d33a" v-if="row.checked" size="24"></uni-icons>

						</view>
					</view>
				</u-collapse-item>
			</u-collapse>
		</view>

	</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				OpenCollapseData: '',
				ListData: [],
				pageParam: {},
			};
		},
		onLoad() {
			let that = this;
			this.pageParam = this.$store.state.BaseScreenParm;

			// 深拷贝参数数据
			let data = JSON.parse(JSON.stringify(this.pageParam.param.data));

			// 检查 ChoiceDataData 是否为空
			if (!this.$utils.isEmpty(this.pageParam.serverSearch.ChoiceDataData)) {
				this.OpenCollapseData = this.pageParam.serverSearch.ChoiceDataData.ID;

				// 遍历数据项
				for (let item of data) {
					// 检查匹配项
					if (item[this.pageParam.serverSearch.ChoiceDataKey] == this.pageParam.serverSearch.ChoiceDataData[this
							.pageParam.serverSearch.ChoiceDataKey]) {
						// 遍历子项
						for (let row of item['Child']) {
							// 检查匹配的子项
							if (row[this.pageParam.serverSearch.ChoiceDataKey] == this.pageParam.serverSearch
								.ChoiceDataData['Child'][0][this.pageParam.serverSearch.ChoiceDataKey]) {
								// 设置 checked 为 true
								row['checked'] = true;
								break;
							}
						}
					}
				}
			}

			// 深拷贝数据到 ListData
			this.ListData = JSON.parse(JSON.stringify(data));

		},
		beforeDestroy() {
			let that = this;
			this.$store.commit('ChangeAndSetStateData', {
				Name1: 'BaseScreenParm',
				data: {}
			})
		},
		methods: {
			ChoiceCell(item, row) { // 选中数据
				let that = this;
				this.ListData.forEach(ReItem => {
					ReItem.Child.map(ReRow => {
						ReRow.checked = false;
					})
				})

				row.checked = true;

				let data = deepClone(item);

				data['Child'] = [];
				data['Child'].push(row);

				uni.$emit(this.pageParam.ListeningName, {
					type: that.pageParam.type,
					msg: '',
					data: data
				});

				uni.navigateBack();
			},
		},
	}

	function deepClone(obj) {
		if (obj === null || typeof obj !== 'object') {
			return obj;
		}

		let clone = Array.isArray(obj) ? [] : {};

		for (let key in obj) {
			if (obj.hasOwnProperty(key)) {
				clone[key] = deepClone(obj[key]);
			}
		}

		return clone;
	}
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		height: 100vh;
		background-color: #f9f9f9;

		.MainBox {
			flex: 1;
			overflow: auto;
		}
	}
</style>